import { resolve } from 'path';
import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

// 获取当前环境
const isDevelopment = process.env.NODE_ENV !== 'production';

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
  },
  preload: {
    plugins: [externalizeDepsPlugin()],
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src'),
      },
    },
    plugins: [
      vue(),
      // 自动导入Element Plus组件
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      // 按需引入Element Plus组件
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    server: isDevelopment
      ? {
          // 只在开发环境启用代理
          proxy: {
            '/api': {
              target: 'https://wsapp.globalshhop.top',
              changeOrigin: true,
              rewrite: (path) => path.replace(/^\/api/, ''),
            },
          },
        }
      : {},
  },
});
